﻿using Microsoft.EntityFrameworkCore;
using Net5ApiAndEfMySql.DtoModel;
using Net5ApiAndEfMySql.IRepository.SystemAdmin;
using Net5ApiAndEfMySql.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VueAndNetCoreEfApi.IRepository.SystemAdmin;
using VueAndNetCoreEfApi.PoModel.SystemAdmin;

namespace VueAndNetCoreEfApi.Repository.SystemAdmin
{
    public class LineRepository : BaseRepository, ILineRepository
    {
        private IRoleDbRepository _IRoleDbRepository;

        public LineRepository(IRoleDbRepository IRoleDbRepository)
        {
            _IRoleDbRepository = IRoleDbRepository;
        }
        public bool AddLinePo(LinePo lp)
        {
            if (lp == null) return false;
            SoaSystemCenterDbContext.Add(lp);
            SoaSystemCenterDbContext.SaveChanges();
            return true;
        }

        public bool DeleteLinePo(int line_id)
        {
            if (line_id < 0) return false;
            var dbuser = SoaSystemCenterDbContext.LinePo.FirstOrDefault(c => c.line_id == line_id);
            if (dbuser == null)
            {
                return false;
            }
            //删除
            string delsql = "delete from Line where line_id=@p0";
            SoaSystemCenterDbContext.Database.ExecuteSqlRaw(delsql, dbuser.line_id);
            SoaSystemCenterDbContext.SaveChanges();
            return true;
        }

        public PagerResult<LinePo> GetPageList(SearchModel sm)
        {
            IQueryable<LinePo> op = SoaSystemCenterDbContext.LinePo.AsQueryable();

            if (sm.SearchItem != null && sm.SearchItem.Count > 0)
            {
                var tempDi = sm.SearchItem;

                string line_name = "line_name";
                if (tempDi.Keys.Contains(line_name) && !string.IsNullOrEmpty(tempDi[line_name]))
                {
                    op = op.Where(c => c.line_name.Contains(tempDi[line_name]));
                }
                string line_start = "line_start";
                if (tempDi.Keys.Contains(line_start) && !string.IsNullOrEmpty(tempDi[line_start]))
                {
                    op = op.Where(c => c.line_start.Contains(tempDi[line_start]));
                }
                string line_end = "line_end";
                if (tempDi.Keys.Contains(line_end) && !string.IsNullOrEmpty(tempDi[line_end]))
                {
                    op = op.Where(c => c.line_end.Contains(tempDi[line_end]));
                }

            }
            return Pager(op, sm);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="op"></param>
        /// <returns></returns>
        public bool UpdateLinePo(OutsourcePo op)
        {
            if (op == null) return false;
            SoaSystemCenterDbContext.Update(op);
            SoaSystemCenterDbContext.SaveChanges();
            return true;
        }
    }
}
